import {reqListMerchandiseByCategory, reqLogin, reqGetCategoryInfo} from '../../api/index'
import { toast } from '../../utils/extendApi';
import {setStorage} from '../../utils/storage'
Page({
    data: {
      show:'true',
        // 页面数据
      goods: [],
      active:'',
      category:[]
        
      },
      navigateToSearchPage: function() {
        wx.navigateTo({
          url: '/pages/search/search', // 替换为你的搜索页面路径
        });
        
      },

    onLoad(){
      wx.login({
        success: async ({code}) => {
          // console.log(code)
          const {data} = await reqLogin({'code':code})
          // console.log(data)
          setStorage('token',data.token)
          setStorage('openId',data.openid)
          setStorage('customerId',data.id)
          this.setCategory()
        }
      })
    },


    navigateToLivesquarePage:function(){
      wx.redirectTo({
        url: '/pages/livesquare/livesquare',
      })
    },

    // 修改目录名称后展示不同目录下的商品
    onChange(event){
      this.setData({
        active:event.detail.name
      })
      // console.log(this.data.active)
      this.showMerchandise()
    },

    async showMerchandise(){
      const {data} = await reqListMerchandiseByCategory(this.data.active)
      // console.log(data)

      this.setData({
        goods:data
      })
    },

    navigateToGoodDetail:function(e){
      const id = e.currentTarget.dataset.productid
      // console.log(e.currentTarget.dataset.productid)

      //业务类型为3表示用户是从平台直接进入商品界面
      const businessType = 3
      wx.navigateTo({
        url: `/pages/good/good?productId=${id}&businessType=${businessType}`,
      })
    },

    async setCategory(){
      const {data} = await reqGetCategoryInfo()
      this.setData({
        category:data,
        active:data[0].productId
      })

      this.showMerchandise()
    }
    
})